﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class ManageTest : System.Web.UI.Page
{
    DataTable dt;
    DataSet dsTest = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadSubject();
            
        }
    }

    private void bindGrid()
    {

        dsTest = AdminCreateTestDAL.GetTestInfoBasedOnSubTopic(Convert.ToInt32(ddlSub.SelectedValue), Convert.ToInt32(ddlTopic.SelectedValue));
        dsTest.Tables[0].Columns.Add("Topics", typeof(System.String));
        bindTopics();
        gvTest.DataSource = dsTest.Tables[0];
        //gvTest.Columns[3].ItemStyle.Width = 50;
        gvTest.DataBind();
    }

    private void bindTopics()
    {
        int i, j;
        string topics;

        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            for (j = 0; j < dsTest.Tables[1].Rows.Count; j++)
            {
                if (dsTest.Tables[0].Rows[i][0].ToString() == dsTest.Tables[1].Rows[j][0].ToString())
                    dsTest.Tables[0].Rows[i]["Topics"] += dsTest.Tables[1].Rows[j][1] + ",";
            }
        }
        for (i = 0; i < dsTest.Tables[0].Rows.Count; i++)
        {
            topics = dsTest.Tables[0].Rows[i]["Topics"].ToString();
            topics = topics.Substring(0, topics.Length - 1);
            dsTest.Tables[0].Rows[i]["Topics"] = topics;
        }
    }

    private void LoadSubject()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        ddlSub.Items.Add("--Select One--");
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByText("--Select One--"));
        ListItem list = new ListItem();
        list.Text = "All";
        list.Value = "0";
        ddlSub.Items.Add(list);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }
    }

    protected void ddlSub_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlTopic.Items.Clear();
        if (ddlSub.SelectedItem.Text == "--Select One--")
        {
            lablError.Text = "Please select one subject";

        }
        else
        {
            dt = new DataTable();
            ListItem list = new ListItem();
            list.Text = "All";
            list.Value = "0";
            ddlTopic.Items.Add(list);
            dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][0].ToString();
                li.Value = dt.Rows[i][1].ToString();
                ddlTopic.Items.Add(li);
            }
        }
    }

    protected void gvTest_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTest.PageIndex = e.NewPageIndex;
        gvTest.SelectedIndex = -1;
        bindGrid();
    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
        bindGrid();
    }
    protected void ddlTopic_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ClearRecord_Click(object sender, EventArgs e)
    {
        gvTest.DataSource = null;
        gvTest.DataBind();
    }

    protected void gvTest_SelectedIndexChanged(object sender, EventArgs e)
    {
        int testId = Convert.ToInt32(gvTest.SelectedDataKey.Value);
        DataTable dtTest = new DataTable();
        dtTest = AdminCreateTestDAL.GetTestStatusInfoBasedOnTestId(Convert.ToInt32(testId));
        //if (string.Compare(dtTest.Rows[0][0].ToString(), "Scheduled") == 0)
        string TestStatus = "";
        TestStatus = dtTest.Rows[0][0].ToString().Trim();

        if (TestStatus.Trim().ToLower() == "Scheduled".Trim().ToLower())
        //if(string.Compare(TestStatus.Trim(),"Scheduled")==0)
        {
            Response.Redirect("~/Admin/CreateTestPageOne.aspx?TestId=" + testId.ToString());
        }
    }

    protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int testId = Convert.ToInt32(e.CommandArgument); 
        switch (e.CommandName)    
        {
            case "btnCopyClick": Response.Redirect("~/Admin/CreateTestPageOne.aspx?CopyTestId=" + testId.ToString()); 
            break;

            case "btnLinkClick": Response.Redirect("~/Admin/ViewTestReport.aspx?TestId=" + testId.ToString());
            break;

            case "btnLinkRankClick": Response.Redirect("../Student/ViewTestRank.aspx?TestId=" + testId.ToString());
            break;
        }  

    }
}